﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections.Specialized;

namespace SuchSofts.OpenImage.Lib.Image
{
    internal class ImageFilter
    {

        public string Types { get; set; }
        public Size MinimumSize { get; set; }
        public Size MaximumSize { get; set; }

        public ImageFilter()
        {
            this.Types = GenerateTypesString();
            this.MinimumSize = new Size(1, 1);
            this.MaximumSize = new Size(int.MaxValue, int.MaxValue);
        }

        public ImageFilter(string types, Size minimumSize, Size maximumSize)
        {
            this.Types = types;
            this.MinimumSize = minimumSize;
            this.MaximumSize = maximumSize;
        }

        public ImageFilter(IList<string> types, Size minimumSize, Size maximumSize)
        {
            this.Types = GenerateTypesString(types);
            this.MinimumSize = minimumSize;
            this.MaximumSize = maximumSize;
        }

        #region Static Memebers

        public static readonly List<string> AllTypes = new List<string> { "jpg", "gif", "png", "bmp","{ALL}" };

        public static string GenerateTypesString()
        {
            return GenerateTypesString(AllTypes);
        }

        public static string GenerateTypesString(IList<string> types)
        {
            if (types.Count == 0)
            {
                return "";
            }

            StringBuilder typesBuilder = new StringBuilder();
            for (int i = 0; i < types.Count - 1; i++)
            {
                typesBuilder.Append(types[i]);
                typesBuilder.Append("|");
                for (int j = i + 1; j < types.Count; j++)
                {
                    if (types[j].Equals(types[i], StringComparison.OrdinalIgnoreCase))
                    {
                        types.RemoveAt(j);
                    }
                }
            }
            typesBuilder.Append(types[types.Count - 1]);
            return typesBuilder.ToString();
        }

        public static string GenerateTypesString(string[] types)
        {
            List<string> typesCollection = new List<String>();
            foreach (string type in types)
            {
                typesCollection.Add(type);
            }
            return GenerateTypesString(typesCollection);
        }

        public static bool IsLeagalTypesString(ref string typesString)
        {
            string[] types = typesString.Split('|');
            if (types.Length == 0)
            {
                return false;
            }
            foreach (string type in types)
            {
                bool notMatch = true;
                foreach (string legalType in AllTypes)
                {
                    if (type.Equals(legalType, StringComparison.OrdinalIgnoreCase))
                    {
                        notMatch = false;
                        break;
                    }
                }
                if (notMatch)
                {
                    return false;
                }
            }
            typesString = GenerateTypesString(types);
            return true;
        }

        #endregion

    }
}
